<template>
    <view>
        <slot name="top"></slot>
        <list :list="data.list" empty_list_title="还没有资产日志">
            <template #list>
                <view class="cu-timeline">
                    <template v-for="(info, key) in data.list">
                        <view class="cu-time" v-if="(key==0) || (data.list[key].created_date != data.list[key-1].created_date)">
                            {{info.created_at | dateFormat('yyyy-MM-dd')}}
                        </view>
                        <view class="cu-item">
                            <view class="content bg-white">
                                <view class="cu-capsule radius">
                                    <view class="cu-tag bg-nddyny">
                                        {{info.created_at | dateFormat('hh:mm:ss')}}
                                    </view>
                                    <view class="cu-tag line-nddyny">
                                        {{$store.state.user.user_asset_log_types[info.action_type]}}
                                    </view>
                                </view>
                                <view class="margin-top-30">
                                    <view v-if="info.is_empty_change">
                                        <text class="text-gray">没有资产变动</text>
                                    </view>
                                    <view v-else>
                                        <view class="flex align-center" v-for="(name, key) in asset_key_list" :key="key"
                                            v-if="info['change_'+key] != 0">
                                            <view class="min-width-em4">{{name}}</view>
                                            <view class="flex align-center">
                                                <text>{{info[key] | bc('sub', info['change_'+key]) | floor}}</text>
                                                <text class="text-sm"><text class="cuIcon-right margin-lr-10"></text></text>
                                                <text>{{info[key] | floor}}</text>
                                                <text class="margin-left-10" v-if="info['change_'+key] != 0" :class="info['change_'+key] > 0 ? 'text-red' : (info['change_'+key] < 0 ? 'text-green' : '')">
                                                    <text :class="info['change_'+key] > 0 ? 'cuIcon-triangleupfill' : (info['change_'+key] < 0 ? 'cuIcon-triangledownfill' : '')"></text>
                                                    {{info[key] | bc3('sub', info[key], 'sub', info['change_'+key], 6) | floor(4)}}
                                                </text>
                                            </view>
                                        </view>
                                    </view>
                                </view>
                            </view>
                        </view>
                    </template>
                </view>
            </template>
        </list>
    </view>
</template>

<script>
    export default {
        props: ['parentFormtag', 'parentForm'],
        data() {
            return {
                data: {
                    offset: 0,
                    limit: 15,
                    stop: false,
                    list: [],
                },
                asset_key_list: {
                    goods_amount: '货款',
                    gold_share_amount: '金股',
                    gold_coupon_amount: '金券',
                    gold_bean_amount: '金豆',
                    advpay_amount: '消费余额',
                    balance_amount: '提现红包',
                    voucher_amount: '消费红包',
                    promote_amount: '推广奖励',
                    invest_share_amount: '股权',
                    credit_score: '信用分',
                }
            }
        },
        created() {
            this.getList(this.$api.list_action_init);
        },
        methods: {
            getList(action, success_callback) {
                if (this.$store.state.user.user_asset_log_types) {
                    this._getList(action, success_callback);
                    return;
                }
                this.$api.post(this, {
                    url: '/user/asset/logTypes',
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        this.$store.state.user.user_asset_log_types = Result.result;
                        this._getList(action, success_callback);
                    }
                });
            },
            _getList(action, success_callback) {
                this.$api.list(this, {
                    action,
                    url: '/user/asset/logList',
                    data_append: this.parentForm,
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        Result.result.list.forEach((info) => {
                            info.is_empty_change = true;
                            Object.keys(this.asset_key_list).forEach(key => {
                                if (info['change_' + key] != 0) {
                                    info.is_empty_change = false;
                                }
                            })
                        })
                        this.data.list = this.data.list.concat(Result.result.list);
                        if (success_callback) success_callback();
                    }
                });
            }
        },
    }
</script>
